package com.myamap.tool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.CrossApp.lib.CrossAppActivity;
import org.CrossApp.lib.CrossAppHelper;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.maps2d.AMap.OnInfoWindowClickListener;
import com.amap.api.maps2d.AMap.OnMarkerClickListener;
import com.amap.api.maps2d.AMap.OnMarkerDragListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.AMap.OnMapClickListener;
import com.amap.api.maps2d.AMap.OnMapLoadedListener;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.overlay.WalkRouteOverlay;
import com.amap.api.services.busline.BusStationItem;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.BusStep;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.DriveStep;
import com.amap.api.services.route.RailwayStationItem;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkStep;
import com.amap.api.services.route.RouteSearch.BusRouteQuery;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.RouteSearch.WalkRouteQuery;
import com.amap.api.services.route.WalkRouteResult;
import com.d6td.test.R;
import com.myamap.util.AMapUtil;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

/*
 * 高德2D地图包装类
 * 
 */
public class MyAmapView implements OnMapLoadedListener, OnMapClickListener, OnMarkerDragListener, OnMarkerClickListener, OnInfoWindowClickListener, AMapLocationListener, OnRouteSearchListener
{
	public enum RouteViewType
	{
		DriveRoute(1), BusRoute(2), WalkRoute(3);
		
		private int _value;

	    private RouteViewType(int value)
	    {
	        _value = value;
	    }

	    public int value()
	    {
	        return _value;
	    }
	    
	    public static RouteViewType getRouteViewType(int value)
	    {
	    	switch(value)
	    	{
		    	case 1:
		    	default:
		    		return DriveRoute;
		    	case 2:
		    		return BusRoute;
		    	case 3:
		    		return WalkRoute;
	    	}
	    }
	};
	
	private RouteViewType mRouteViewType;
	
	private RouteSearch.FromAndTo mFromAndTo;
	
	private RouteSearch mRouteSearch;
	
	private BusRouteResult mBusRouteResult;
	
	private MapView mAmapView;
	
	private int miViewTag;
	
	private double mdLat;
	private double mdLng;
	
	private boolean mbAmapLoaded;
	
	private boolean mbLoc;
	
	private static Button mLocationBt;
	
	private AMapLocationClient mLocationClient;
	
	private Marker mLocMarker;
	
	private Marker mnewMarker;
	
	private boolean mbRouteSearch;
	
	private String mBusRouteCity;
	private String mBusRouteCityD;
	
	private int mfX;
	private int mfY;
	private int mfW;
	private int mfH;
	
	private HashMap<String, Marker> s_Markers = new HashMap<String, Marker>();
	
	public MyAmapView(Context context, int viewTag, Bundle bundle, float x, float y, float width, float height) 
	{
		mfX = (int) x;
		mfY = (int) y;
		mfW = (int) width;
		mfH = (int) height;
		
		mRouteViewType = RouteViewType.DriveRoute;
		
    	mdLat = 39.9;
    	mdLng = 116.3;
    	mbAmapLoaded = false;
    	mbLoc = false;
    	mbRouteSearch = false;
    	
    	mnewMarker = null;
    	
    	miViewTag = viewTag;
    	
    	FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.LEFT|Gravity.TOP;
        layoutParams.leftMargin = (int)x;
        layoutParams.topMargin = (int)y;
        layoutParams.width = (int)width;
        layoutParams.height = (int)height;
		mAmapView = new MapView(CrossAppActivity.getContext());
		mAmapView.onCreate(bundle);
    	CrossAppActivity.getFrameLayout().addView(mAmapView, layoutParams);
    	
    	FrameLayout.LayoutParams locbtParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
		
		locbtParams.gravity = Gravity.LEFT|Gravity.TOP;
		locbtParams.leftMargin = (int) (width - 80 * (CrossAppHelper.getDPI() / 320) - 20);
		locbtParams.topMargin = 20;
		locbtParams.width = (int) (80 * (CrossAppHelper.getDPI() / 320));
		locbtParams.height = (int) (80 * (CrossAppHelper.getDPI() / 320));
    	mLocationBt = new Button(CrossAppActivity.getContext());
    	mLocationBt.setBackgroundResource(R.drawable.location_selected2d);
    	mAmapView.addView(mLocationBt, locbtParams);
    	
    	initListener();
    	
    	initLocation();
    	
    	initRouteSearch();
	}
	
	private void initListener()
	{
    	//加载完成回调
    	mAmapView.getMap().setOnMapLoadedListener(this);
		//点击地图回调
    	mAmapView.getMap().setOnMapClickListener(this);
    	//marker拖拽回调
    	mAmapView.getMap().setOnMarkerDragListener(this);
    	//marker拖拽回调
    	mAmapView.getMap().setOnMarkerClickListener(this);
    	//marker拖拽回调
    	mAmapView.getMap().setOnInfoWindowClickListener(this);
	}
	
	private void initLocation()
	{
		//Location Marker
		MarkerOptions markeroption = new MarkerOptions();
		markeroption.draggable(false);
		markeroption.title("我的位置");		
		markeroption.icon(BitmapDescriptorFactory.fromResource(R.drawable.green_imv));
		mLocMarker = mAmapView.getMap().addMarker(markeroption);
		
		//location
    	mLocationClient = new AMapLocationClient(CrossAppActivity.getContext());
		AMapLocationClientOption locationOption = new AMapLocationClientOption();
		locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
		locationOption.setOnceLocation(true);
		locationOption.setNeedAddress(true);
		mLocationClient.setLocationOption(locationOption);
		mLocationClient.setLocationListener(this);
    	
        mLocationBt.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
            	if (mLocationClient != null)
            	{
            		mLocationClient.startLocation();
            	}
            }
        });
	}
	
	private void initRouteSearch()
	{
		mRouteSearch = new RouteSearch(CrossAppActivity.getContext());
		mRouteSearch.setRouteSearchListener(this);
		
		//默认是起点北京大学 终点是清华大学
		mFromAndTo = null;
		
		mBusRouteResult = null;
		
		mBusRouteCity = "";
		mBusRouteCityD = "";
	}
	
	public void destroy()
	{
		if (mLocMarker != null)
		{
			mLocMarker.remove();
			mLocMarker.destroy();
		}
		
		if (mLocationClient != null)
    	{
			mLocationClient.stopLocation();
			mLocationClient.onDestroy();
			mLocationClient = null;
		}
		
		CrossAppActivity.getFrameLayout().removeView(mAmapView);
		clearMarkers();
		mAmapView.removeAllViews();
		mAmapView.getMap().clear();
		mAmapView.onDestroy();
	}
	
	public void setRouteViewType(double slat, double slng, double elat, double elng, int value)
	{
		mRouteViewType = RouteViewType.getRouteViewType(value);
		mFromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(slat, slng), new LatLonPoint(elat, elng));
	}
	
	public void setBusRouteCity(String city, String cityd)
	{
		mBusRouteCity = city;
		mBusRouteCityD = cityd;
	}
	
	public void searchRoute()
	{
		mbRouteSearch = true;
		
		if (mbAmapLoaded)
		{
			mbRouteSearch = false;
			
			switch(mRouteViewType.value())
			{
				// 驾车路径规划
				case 1:
				default:
				{
					DriveRouteQuery query = new DriveRouteQuery(mFromAndTo, RouteSearch.DrivingDefault, null, null, "");// 第一个参数表示路径规划的起点和终点，第二个参数表示驾车模式，第三个参数表示途经点，第四个参数表示避让区域，第五个参数表示避让道路
					mRouteSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
					
					break;
				}
				//公交车路线规划
				case 2:
				{
					if (mBusRouteCity != null && !mBusRouteCity.equals(""))
					{
						if (mBusRouteCity.equals(mBusRouteCityD))
						{
							BusRouteQuery query1 = new BusRouteQuery(mFromAndTo, RouteSearch.BusDefault, mBusRouteCity, 0);// 第一个参数表示路径规划的起点和终点，第二个参数表示公交查询模式，第三个参数表示公交查询城市区号，第四个参数表示是否计算夜班车，0表示不计算
							mRouteSearch.calculateBusRouteAsyn(query1);// 异步路径规划公交模式查询
						}
						else
						{
							BusRouteQuery query2 = new BusRouteQuery(mFromAndTo, RouteSearch.BusDefault, mBusRouteCity, 0);// 第一个参数表示路径规划的起点和终点，第二个参数表示公交查询模式，第三个参数表示公交查询城市区号，第四个参数表示是否计算夜班车，0表示不计算
							query2.setCityd(mBusRouteCityD);
							mRouteSearch.calculateBusRouteAsyn(query2);// 异步路径规划公交模式查询
						}
						
						mBusRouteCity = "";
						mBusRouteCityD = "";
					}
					
					break;
				}
				//步行
				case 3:
				{
					WalkRouteQuery query = new WalkRouteQuery(mFromAndTo, RouteSearch.WalkDefault);
					mRouteSearch.calculateWalkRouteAsyn(query);// 异步路径规划步行模式查询
					
					break;
				}
			}
		}
	}
	
	public void showBusRouteView(int bustag)
	{
		if (mBusRouteResult != null)
		{
			mAmapView.getMap().clear();// 清理地图上的所有覆盖物
			
			BusRouteOverlay busrouteOverlay = new BusRouteOverlay(CrossAppActivity.getContext(), mAmapView.getMap(),
					mBusRouteResult.getPaths().get(bustag), mBusRouteResult.getStartPos(),
					mBusRouteResult.getTargetPos());
			busrouteOverlay.addToMap();
			busrouteOverlay.zoomToSpan();
		}
	}
	
	private int[] toIntArray(List<Integer> intList)
	{
		int[] ret = new int[intList.size()];
	    int i = 0;
	    for(Integer intValue : intList)
	    {
	    	ret[i] = intValue.intValue();
	    	i++;
	    }
	    return ret;
	}
	
	private float[] toFloatArray(List<Float> floatList)
	{
		float[] ret = new float[floatList.size()];
	    int i = 0;
	    for(Float floateValue : floatList)
	    {
	    	ret[i] = floateValue.floatValue();
	    	i++;
	    }
	    return ret;
	}
	
	public void getBusRouteStep(int bustag)
	{
		if (mBusRouteResult != null)
		{
			BusPath path = mBusRouteResult.getPaths().get(bustag);
			
			if (path != null)
			{
				int size = path.getSteps().size();
				
				List<Integer> typeArray = new ArrayList<Integer>();
				List<String> nameArray = new ArrayList<String>();
				List<Float> distanceArray = new ArrayList<Float>();
				List<Float> durationArray = new ArrayList<Float>();
				List<String> stationsArray = new ArrayList<String>();
				
				for (int i = 0; i < size; ++i)
				{
					BusStep step = path.getSteps().get(i);
					
					if (step.getWalk() != null && step.getWalk().getDistance() > 0)
					{
						int type = 1;
						String name = "步行" + (int) step.getWalk().getDistance() + "米";
						float distance = step.getWalk().getDistance();
						float duration = step.getWalk().getDuration();
						String station = "";
						
						typeArray.add(type);
						nameArray.add(name);
						distanceArray.add(distance);
						durationArray.add(duration);
						stationsArray.add(station);
					}
					
					if (step.getBusLines() != null && step.getBusLines().size() > 0)
					{
						int type = 2;
						String name = step.getBusLines().get(0).getBusLineName();
						float distance = step.getBusLines().get(0).getDistance();
						float duration = step.getBusLines().get(0).getDuration();
						
						StringBuffer stationbuffer = new StringBuffer();
						
						stationbuffer.append(step.getBusLines().get(0).getDepartureBusStation().getBusStationName());
						stationbuffer.append("/");
						
						for (BusStationItem busline : step.getBusLines().get(0).getPassStations()) 
					    {
							if (busline == null) 
						    {
								continue;
						    }
							
						    stationbuffer.append(busline.getBusStationName());
						    stationbuffer.append("/");
					    }
						
						stationbuffer.append(step.getBusLines().get(0).getArrivalBusStation().getBusStationName());
						
						String station = stationbuffer.toString();
						
						typeArray.add(type);
						nameArray.add(name);
						distanceArray.add(distance);
						durationArray.add(duration);
						stationsArray.add(station);
					}

					if (step.getRailway() != null)
					{
						int type = 3;
						String name = step.getRailway().getName();
						float distance = step.getRailway().getDistance();
						float duration = Float.parseFloat(step.getRailway().getTime());
						
						StringBuffer stationbuffer = new StringBuffer();
						
						stationbuffer.append(step.getRailway().getDeparturestop().getName());
						stationbuffer.append("/");
						
						for (RailwayStationItem railline : step.getRailway().getViastops()) 
					    {
							if (railline == null) 
						    {
								continue;
						    }
						  
						    stationbuffer.append(railline.getName());
						    stationbuffer.append("/");
					    }
						
						stationbuffer.append(step.getRailway().getArrivalstop().getName());
						
						String station = stationbuffer.toString();
						
						typeArray.add(type);
						nameArray.add(name);
						distanceArray.add(distance);
						durationArray.add(duration);
						stationsArray.add(station);
					}
					
					if (step.getTaxi() != null)
					{
						int type = 4;
						String name = "打车到终点";
						float distance = step.getTaxi().getDistance();
						float duration = step.getTaxi().getDuration();
						String station = "";
						
						typeArray.add(type);
						nameArray.add(name);
						distanceArray.add(distance);
						durationArray.add(duration);
						stationsArray.add(station);
					}
				}	
				
				MyAmapViewHelper.onBusRouteSteps(miViewTag, 0, bustag, toIntArray(typeArray), nameArray.toArray(), toFloatArray(distanceArray), toFloatArray(durationArray), stationsArray.toArray());
			}
			else
			{
				MyAmapViewHelper.onBusRouteSteps(miViewTag, -1, bustag, null, null, null, null, null);
			}
		}
		else
		{
			MyAmapViewHelper.onBusRouteSteps(miViewTag, -1, bustag, null, null, null, null, null);
		}
	}
	
	public void setVisible(boolean visible)
	{
		mAmapView.setVisibility(visible ? View.VISIBLE : View.GONE);
	}
	
	public void setMapCenter(double lat, double lng)
	{
		mbLoc = false;
		
		mdLat = lat;
		mdLng = lng;
		
		if (mbAmapLoaded)
		{
			if (mAmapView.getMap() != null)
    		{         
    			mAmapView.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mdLat, mdLng), mAmapView.getMap().getCameraPosition().zoom));
    		}
		}
	}
	
	public void startLocation()
	{
		if (mLocationClient != null)
    	{
			mbLoc = true;
    		mLocationClient.startLocation();
    	}
	}
	
	public void setMapZoomLevel(double level)
	{
		if (mAmapView.getMap() != null)
		{
			mAmapView.getMap().moveCamera(CameraUpdateFactory.zoomTo((float) level));
		}
	}
	
	public void setAMapRect(int x, int y, int width, int height)
	{
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.LEFT|Gravity.TOP;
        layoutParams.leftMargin = x;
        layoutParams.topMargin = y;
        layoutParams.width = width;
        layoutParams.height = height;
        mAmapView.setLayoutParams(layoutParams);
		
        FrameLayout.LayoutParams locbtParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
		
		locbtParams.gravity = Gravity.LEFT|Gravity.TOP;
		locbtParams.leftMargin = (int) (width - 80 * (CrossAppHelper.getDPI() / 320) - 20);
		locbtParams.topMargin = 20;
		locbtParams.width = (int) (80 * (CrossAppHelper.getDPI() / 320));
		locbtParams.height = (int) (80 * (CrossAppHelper.getDPI() / 320));
		
		mLocationBt.setLayoutParams(locbtParams);
	}
	
	public void addMarker(String marktag, String img, boolean dragable, String title, String content, double lat, double lng)
	{
		MarkerOptions markeroption = new MarkerOptions();
		markeroption.draggable(dragable);
		markeroption.title(title);
		markeroption.snippet(content);
		markeroption.position(new LatLng(lat, lng));
		if (img != null && img.equals("") && img != "")
		{
			try
			{
				if (CrossAppActivity.getContext().getAssets().open(img) != null)
				{
					markeroption.icon(BitmapDescriptorFactory.fromAsset(img));
				}
			} 
			catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				markeroption.icon(BitmapDescriptorFactory.fromResource(R.drawable.green_imv));
			}
		}
		else
		{
			markeroption.icon(BitmapDescriptorFactory.fromResource(R.drawable.green_imv));
		}
		
		Marker marker = mAmapView.getMap().addMarker(markeroption);
		
		if (mbAmapLoaded)
		{
			marker.showInfoWindow();
		}
		else
		{
			mnewMarker = marker;
		}
	}
	
	public void deleteMarker(String marktag)
	{
		Marker marker = s_Markers.get(marktag);
		if (marker != null)
		{
			s_Markers.remove(marktag);
			if (marker.isInfoWindowShown())
			{
				marker.hideInfoWindow();
			}
			marker.remove();
			marker.destroy();
		}
	}
	
	public void clearMarkers()
	{
		for (String key1:s_Markers.keySet())
		{
			Marker marker = s_Markers.get(key1);
			if (marker != null)
			{
				if (marker.isInfoWindowShown())
				{
					marker.hideInfoWindow();
				}
				marker.remove();
				marker.destroy();
			}
		}
		
		s_Markers.clear();
		
		mAmapView.getMap().clear();
	}
	
	//OnMapClickListener
	@Override
	public void onMapClick(LatLng arg0) 
	{
		// TODO Auto-generated method stub
		MyAmapViewHelper.onAmapClicked(miViewTag, arg0.latitude, arg0.longitude);
	}

	//OnMapLoadedListener
	@Override
	public void onMapLoaded()
	{
		// TODO Auto-generated method stub
		mbAmapLoaded = true;
		MyAmapViewHelper.onAmapLoaded(miViewTag);

		if (mAmapView.getMap() != null)
		{
			if (mbLoc)
			{
				mLocMarker.setPosition(new LatLng(mdLat, mdLng));
			}
			
			if (mbRouteSearch)
			{
				searchRoute();
			}
			
			if (mnewMarker != null)
			{
				mnewMarker.showInfoWindow();
			}
			
			mAmapView.getMap().moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(mdLat, mdLng)));
		}
	}

	//OnMarkerDragListener
	@Override
	public void onMarkerDrag(Marker arg0)
	{
		// TODO Auto-generated method stub
		String key = null;
		for (String key1:s_Markers.keySet())
		{
			if (s_Markers.get(key1).equals(arg0))
			{
				key = key1;
				break;
			}
		}
		if (key != null)
		{
			MyAmapViewHelper.onAmapMarkerDrag(miViewTag, key, arg0.getPosition().latitude, arg0.getPosition().longitude);
		}
	}

	//OnMarkerDragListener
	@Override
	public void onMarkerDragEnd(Marker arg0)
	{		
		// TODO Auto-generated method stub
		String key = null;
		for (String key1:s_Markers.keySet())
		{
			if (s_Markers.get(key1).equals(arg0))
			{
				key = key1;
				break;
			}
		}
		if (key != null)
		{
			MyAmapViewHelper.onAmapMarkerDragEnd(miViewTag, key, arg0.getPosition().latitude, arg0.getPosition().longitude);
		}
	}

	//OnMarkerDragListener
	@Override
	public void onMarkerDragStart(Marker arg0) 
	{
		// TODO Auto-generated method stub
		String key = null;
		for (String key1:s_Markers.keySet())
		{
			if (s_Markers.get(key1).equals(arg0))
			{
				key = key1;
				break;
			}
		}
		if (key != null)
		{
			MyAmapViewHelper.onAmapMarkerDragStart(miViewTag, key, arg0.getPosition().latitude, arg0.getPosition().longitude);
		}
	}

	//OnMarkerClickListener
	@Override
	public boolean onMarkerClick(Marker arg0) 
	{
		String key = null;
		for (String key1:s_Markers.keySet())
		{
			if (s_Markers.get(key1).equals(arg0))
			{
				key = key1;
				break;
			}
		}
		if (key != null)
		{
			MyAmapViewHelper.onAmapMarkerClick(miViewTag, key);
		}
		return false;
	}

	//OnInfoWindowClickListener
	@Override
	public void onInfoWindowClick(Marker arg0)
	{
		// TODO Auto-generated method stub
		String key = null;
		for (String key1:s_Markers.keySet())
		{
			if (s_Markers.get(key1).equals(arg0))
			{
				key = key1;
				break;
			}
		}
		if (key != null)
		{
			MyAmapViewHelper.onAmapMarkerInfoWindowClick(miViewTag, key);
		}
	}

	//OnLocationChangedListener
	@Override
	public void onLocationChanged(AMapLocation location) 
	{
		// TODO Auto-generated method stub
		int code = 0;
		double lat = 0.0;
		double lng = 0.0;
		String formatAddr = ""; 
		String province = ""; 
		String city = "";
		String district = "";
		String street = ""; 
		String number = "";
		String poi = ""; 
		String adcode = "";
		if (location != null && location.getErrorCode() == 0) 
		{
			code = 0;
			lat = location.getLatitude();
			lng = location.getLongitude();
			
			mdLat = lat;
			mdLng = lng;
			
			formatAddr = location.getAddress(); 
			province = location.getProvince(); 
			city = location.getCity();
			district = location.getDistrict();
			street = location.getStreet(); 
			number = location.getStreetNum();
			poi = location.getPoiName(); 
			adcode = location.getAdCode();

			mLocMarker.setPosition(new LatLng(lat, lng));
			mLocMarker.setTitle(formatAddr);
			mLocMarker.showInfoWindow();
			mAmapView.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 16.0f));
		}
		else 
		{
			code = -2;
		}
		
		mLocationClient.stopLocation();
		
		MyAmapViewHelper.onAmapLocationEnd(miViewTag, code, lat, lng, formatAddr, province, city, district, street, number, poi, adcode);
	}

	@Override
	public void onBusRouteSearched(BusRouteResult result, int errorCode)
	{
		// TODO Auto-generated method stub
		if (mBusRouteResult != null)
		{
			mBusRouteResult = null;
		}
		mBusRouteResult = result;
		
		AMap aMap = mAmapView.getMap();
		aMap.clear();// 清理地图上的所有覆盖物
		
		if (errorCode == AMapException.CODE_AMAP_SUCCESS) 
		{
			if (result != null && result.getPaths() != null) 
			{				
				int size = result.getPaths().size();
				
				double[] slatArray = new double[size];
				double[] slngArray = new double[size];
				double[] elatArray = new double[size];
				double[] elngArray = new double[size];
				
				int[] tagArray = new int[size];
				int[] distanceArray = new int[size];
				int[] durationArray = new int[size];
				float[] costArray = new float[size];
				String[] pathTitleArray = new String[size];
				String[] pathDesArray = new String[size];
				float[] busdisArray = new float[size];
				float[] walkdisArray = new float[size];
				
				for (int i = 0; i < size; ++i)
				{
					BusPath buspath = result.getPaths().get(i);
					
					slatArray[i] = result.getStartPos().getLatitude();
					slngArray[i] = result.getStartPos().getLongitude();
					elatArray[i] = result.getTargetPos().getLatitude();
					elngArray[i] = result.getTargetPos().getLongitude();
					
					tagArray[i] = i;
					distanceArray[i] = (int)buspath.getDistance();
					durationArray[i] = (int)buspath.getDuration();
					costArray[i] = buspath.getCost();
					pathTitleArray[i] = AMapUtil.getBusPathTitle(buspath);
					pathDesArray[i] = AMapUtil.getBusPathDes(buspath);
					busdisArray[i] = buspath.getBusDistance();
					walkdisArray[i] = buspath.getWalkDistance();					
				}
				
				MyAmapViewHelper.onBusRouteSearched(miViewTag, 0, slatArray, slngArray, elatArray, elngArray, tagArray, distanceArray, durationArray, costArray, pathTitleArray, pathDesArray, busdisArray, walkdisArray);
			}
			else
			{
				MyAmapViewHelper.onBusRouteSearched(miViewTag, -1, null, null, null, null, null, null, null, null, null, null, null, null);
			}
		}
		else
		{
			MyAmapViewHelper.onBusRouteSearched(miViewTag, -1, null, null, null, null, null, null, null, null, null, null, null, null);
		}
	}

	@Override
	public void onDriveRouteSearched(DriveRouteResult result, int errorCode)
	{
		// TODO Auto-generated method stub
		AMap aMap = mAmapView.getMap();
		
		aMap.clear();// 清理地图上的所有覆盖物
		
		if (errorCode == AMapException.CODE_AMAP_SUCCESS) 
		{
			if (result != null && result.getPaths() != null) 
			{
				if (result.getPaths().size() > 0) 
				{
					final DrivePath drivePath = result.getPaths().get(0);
					
					DrivingRouteOverLay drivingRouteOverlay = new DrivingRouteOverLay(
							CrossAppActivity.getContext(), aMap, drivePath,
							result.getStartPos(),
							result.getTargetPos(), null);
					drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
					drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况，默认true
					drivingRouteOverlay.addToMap();
					drivingRouteOverlay.zoomToSpan();
					int dis = (int) drivePath.getDistance();
					int dur = (int) drivePath.getDuration();
					String des = AMapUtil.getFriendlyTime(dur) + "(" + AMapUtil.getFriendlyLength(dis) + ")";
					float taxiCost = (int) result.getTaxiCost();
					
					float[] distanceArray = new float[drivePath.getSteps().size()];
					float[] durationArray = new float[drivePath.getSteps().size()];
					String[] instructArray = new String[drivePath.getSteps().size()];
					String[] orientationArray = new String[drivePath.getSteps().size()];
					
					for (int i = 0; i < drivePath.getSteps().size(); ++i)
					{
						DriveStep step = drivePath.getSteps().get(i);
						
						distanceArray[i] = step.getDistance();
						durationArray[i] = step.getDuration();
						instructArray[i] = step.getInstruction();
						orientationArray[i] = step.getOrientation();
					}
					
					MyAmapViewHelper.onDriveRouteSearched(miViewTag, 0, result.getStartPos().getLatitude(), result.getStartPos().getLongitude(), result.getTargetPos().getLatitude(), result.getTargetPos().getLongitude(), dur, dis, taxiCost, des, distanceArray, durationArray, instructArray, orientationArray);
				}
				else
				{
					MyAmapViewHelper.onDriveRouteSearched(miViewTag, -1, -1000, -1000, -1000, -1000, 0, 0, 0, "", null, null, null, null);
				}
			} 
			else 
			{
				MyAmapViewHelper.onDriveRouteSearched(miViewTag, -1, -1000, -1000, -1000, -1000, 0, 0, 0, "", null, null, null, null);
			}
		}
		else 
		{
			MyAmapViewHelper.onDriveRouteSearched(miViewTag, -1, -1000, -1000, -1000, -1000, 0, 0, 0, "", null, null, null, null);
		}
	}

	@Override
	public void onRideRouteSearched(RideRouteResult result, int errorCode) 
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onWalkRouteSearched(WalkRouteResult result, int errorCode) 
	{
		// TODO Auto-generated method stub
		AMap aMap = mAmapView.getMap();
		Context mContext = CrossAppActivity.getContext();
	
		aMap.clear();// 清理地图上的所有覆盖物
		if (errorCode == AMapException.CODE_AMAP_SUCCESS) 
		{
			if (result != null && result.getPaths() != null) 
			{
				if (result.getPaths().size() > 0)
				{
					final WalkPath walkPath = result.getPaths()
							.get(0);
					WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(
							mContext, aMap, walkPath,
							result.getStartPos(),
							result.getTargetPos());
					walkRouteOverlay.addToMap();
					walkRouteOverlay.zoomToSpan();
					int dis = (int) walkPath.getDistance();
					int dur = (int) walkPath.getDuration();
					String des = AMapUtil.getFriendlyTime(dur) + "(" + AMapUtil.getFriendlyLength(dis) + ")";

					float[] distanceArray = new float[walkPath.getSteps().size()];
					float[] durationArray = new float[walkPath.getSteps().size()];
					String[] instructArray = new String[walkPath.getSteps().size()];
					String[] orientationArray = new String[walkPath.getSteps().size()];
					
					for (int i = 0; i < walkPath.getSteps().size(); ++i)
					{
						WalkStep step = walkPath.getSteps().get(i);
						
						distanceArray[i] = step.getDistance();
						durationArray[i] = step.getDuration();
						instructArray[i] = step.getInstruction();
						orientationArray[i] = step.getOrientation();
					}
					
					
					MyAmapViewHelper.onWalkRouteSearched(miViewTag, 0, result.getStartPos().getLatitude(), result.getStartPos().getLongitude(), result.getTargetPos().getLatitude(), result.getTargetPos().getLongitude(), dur, dis, des, distanceArray, durationArray, instructArray, orientationArray);
				} 
				else if (result != null && result.getPaths() == null)
				{
					MyAmapViewHelper.onWalkRouteSearched(miViewTag, -1, -1000, -1000, -1000, -1000, 0, 0, "", null, null, null, null);
				}
			}
			else 
			{
				MyAmapViewHelper.onWalkRouteSearched(miViewTag, -1, -1000, -1000, -1000, -1000, 0, 0, "", null, null, null, null);
			}
		} 
		else 
		{
			MyAmapViewHelper.onWalkRouteSearched(miViewTag, -1, -1000, -1000, -1000, -1000, 0, 0, "", null, null, null, null);
		}
	}
}
